const { getUserId } = require("../utils/userManager.js");
const { http } = require("../utils/http.js");

const addPoints = (amount, type, description, title) => {
  return new Promise((resolve, reject) => {
    const userId = getUserId();
    if (!userId) {
      wx.showToast({ title: '请先登录', icon: 'none' });
      return reject(new Error('用户未登录'));
    }

    const params = {
      userId: userId,
      amount: amount,
      type: type,
      title: title,
      description: description
    };

    http.post('/points/record/change', params)
      .then(res => {
        if (res && res.code === 200) {
          wx.showToast({ title: '操作成功', icon: 'success' });
          resolve(res);
        } else {
          wx.showToast({ title: res.msg || '操作失败', icon: 'none' });
          reject(new Error(res.msg || '操作失败'));
        }
      })
      .catch(err => {
        wx.showToast({ title: '网络请求失败', icon: 'none' });
        reject(err);
      });
  });
};

module.exports = {
  addPoints
};

